package com.audi.adapter;

import java.util.ArrayList;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.audi.R;
import com.audi.dto.DeviceItem;
import com.audi.utils.Constants;

public class StatusDevicesAdapter extends BaseAdapter {
	private Activity mActivity;
	private ArrayList<DeviceItem> mDeviceList;

	public StatusDevicesAdapter(Activity activity,
			ArrayList<DeviceItem> deviceLst) {
		mActivity = activity;
		mDeviceList = deviceLst;
	}

	public int getCount() {
		return mDeviceList.size();
	}

	public DeviceItem getItem(int pos) {
		return mDeviceList.get(pos);
	}

	public long getItemId(int pos) {
		return pos;
	}

	public View getView(int i, View convertView, ViewGroup viewgroup) {
		DeviceItem item = mDeviceList.get(i);
		ViewHolder viewHolder = null;
		if (convertView == null) {
			viewHolder = new ViewHolder();
			convertView = LayoutInflater.from(mActivity).inflate(
					R.layout.row_status_devices, null);
			viewHolder.imgDevices = (ImageView) convertView
					.findViewById(R.id.image_device);
			viewHolder.imgStatus = (ImageView) convertView
					.findViewById(R.id.img_status_on_off);
			viewHolder.txtStatus = (TextView) convertView
					.findViewById(R.id.name_device);
			viewHolder.txtPlaceName = (TextView) convertView
					.findViewById(R.id.name_room);
			viewHolder.lnlStatusDevices = (LinearLayout) convertView
					.findViewById(R.id.lnl_row_status_devices);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}

		viewHolder.imgDevices.setImageResource(R.drawable.icon_device_default);
		viewHolder.txtPlaceName.setText((CharSequence) item.getDevicePlace());
		viewHolder.txtStatus.setText((CharSequence) item.getDeviceName());

		if (item.getDeviceStatus() == Constants.CONTROL_ON_OFF.ON.ordinal()) {
			viewHolder.imgStatus
					.setImageResource(R.drawable.btn_place_card_onoff_on);
		} else if (item.getDeviceStatus() == Constants.CONTROL_ON_OFF.OFF.ordinal()) {
			viewHolder.imgStatus
					.setImageResource(R.drawable.btn_place_card_onoff_off);
		} else if (item.getDeviceStatus() == Constants.CONTROL_ON_OFF.NONE.ordinal()) {
			viewHolder.imgStatus.setImageResource(NO_SELECTION);
		}

		if (item.getDeviceType() == Constants.DEVICE_TYPE.ONOFFPLUG.ordinal()
				|| item.getDeviceType() == Constants.DEVICE_TYPE.ONOFFSWITCH
						.ordinal()
				|| item.getDeviceType() == Constants.DEVICE_TYPE.ONOFFLIGHTING
						.ordinal()) {
			viewHolder.lnlStatusDevices.setAlpha(1f);

		} else {
			viewHolder.lnlStatusDevices.setAlpha(0.3f);
		}

		return convertView;
	}

	static class ViewHolder {
		public ImageView imgDevices, imgStatus;
		public TextView txtStatus, txtPlaceName;
		public LinearLayout lnlStatusDevices;

	}

}
